home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Transport Independent Speech / SpokenSerialApp / Recognition.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-20  |  3.3 KB  |  118 lines  |  [TEXT/CWIE]

  1. #include "Recognition.h"
  2.  
  3. OSErr        MakeNewLanguage        (SpeechInfoPtr theSpeechInfo)
  4. {
  5.     OSErr        theErr        = noErr;
  6.  
  7.     /* Make language models */
  8.     theErr = ReadInLanguages (theSpeechInfo);
  9.  
  10.     /* Use this LM in recognition */
  11.     if (theErr == noErr) {
  12.         theErr = SRSetLanguageModel (theSpeechInfo->theRecognizer, theSpeechInfo->languages->theLanguage);
  13.     }
  14.  
  15.     return theErr;
  16. }
  17.  
  18. OSErr    ReadInLanguages            (SpeechInfoPtr theSpeechInfo)
  19. {
  20.     LanguageModelListPtr    currentLang            = nil;
  21.     TREEResourcePtr            TREEResPtr            = nil;
  22.     short                    numLanguages        = 0,
  23.                             onlySubPath            = false,
  24.                             i;
  25.     OSErr                    theErr                = noErr;
  26.  
  27.     theErr = ResError ();
  28.  
  29.     theSpeechInfo->languages = (LanguageModelListPtr)NewPtr (sizeof (LanguageModelList));
  30.     theErr = AddStringsToLanguage (theSpeechInfo, currentLang, kBaseResID + i);
  31.  
  32.     return theErr;
  33. }
  34.  
  35.  
  36. OSErr    AddStringsToLanguage        (SpeechInfoPtr theSpeechInfo, LanguageModelListPtr currentLang, short resID)
  37. {
  38.     SRLanguageModel            newModel;
  39.     SRPath                    beginningOfPath,
  40.                             restOfPath;
  41.     Str63                    phraseString;
  42.     OSType                    type                = 0;
  43.     TREEResourcePtr            TREEResPtr            = nil;
  44.     CommandPtr                theCommand            = nil;
  45.     long                    refCon                = 0,
  46.                             flags                = 0,
  47.                             ID                    = 0;
  48.     short                    j                    = 1,
  49.                             onlySubPath            = false;
  50.     OSErr                    theErr                = noErr;
  51.     Boolean                    done                = false;
  52.  
  53.     theErr = SRNewLanguageModel (theSpeechInfo->recogSystem, &newModel, nil, 0);
  54.     refCon = resID;
  55.     theErr = SRSetProperty (newModel, kSRRefCon, &refCon, sizeof (refCon));
  56.     theErr = SRAddText (newModel, phraseString+1, phraseString[0], (long)theCommand);
  57.  
  58.     if (theErr != noErr) {    /*    release newly created LM if an error occured while adding phrases */
  59.         SRReleaseObject (newModel);
  60.     }
  61.     else {                    /*    return new LM */
  62.     //We don't release the language models because we will be switching between them later
  63.         currentLang->theLanguage = newModel;
  64.     }
  65.  
  66.     return theErr;
  67. }
  68.  
  69. OSErr        InitSpeechRecognition    (SpeechInfoPtr theSpeechInfo)
  70. {
  71.     OSErr        theErr        = noErr;
  72.  
  73.     /* Make sure SpeechRecognition Toolbox is available */
  74.     theErr = Gestalt (gestaltSpeechRecognitionVersion, &theSpeechInfo->attributes);
  75.     /* Version number must be at least 1.5 to support Speech Recognition Toolbox API used here. */
  76.     if (theErr == noErr) {
  77.         if (theSpeechInfo->attributes < 0x00000150) {
  78.             theErr = kNotEnoughSpeechVersion;
  79.         }
  80.     }
  81.  
  82.     return theErr;
  83. }
  84.  
  85. OSErr        OpenSpeechRecognition    (SpeechInfoPtr theSpeechInfo)
  86. {
  87.     OSErr        theErr        = noErr;
  88.  
  89.     theErr = SROpenRecognitionSystem (&theSpeechInfo->recogSystem, kSRDefaultRecognitionSystemID);
  90.  
  91.     return theErr;
  92. }
  93.  
  94. OSErr        ConfigureRecognition    (SpeechInfoPtr theSpeechInfo)
  95. {
  96.     OSErr        theErr            = noErr;
  97.     short        feedbackModes    = kSRHasFeedbackHasListenModes;
  98.  
  99.     /* We use the default feedback and listening modes */
  100.     theErr = SRSetProperty (theSpeechInfo->recogSystem, kSRFeedbackAndListeningModes, &feedbackModes, sizeof (feedbackModes));
  101.  
  102.     return theErr;
  103. }
  104.  
  105. OSErr        GetNewRecognizer        (SpeechInfoPtr theSpeechInfo)
  106. {
  107.     OSErr        theErr            = noErr;
  108.     Boolean        forground        = false;        //We want to recognize even if in the background
  109.  
  110.     /* Create a recognizer with default speech source -- e.g. the desktop microphone */
  111.     theErr = SRNewRecognizer (theSpeechInfo->recogSystem, &theSpeechInfo->theRecognizer, kSRDefaultSpeechSource);
  112.     if (theErr == noErr) {
  113.         theErr = SRSetProperty (theSpeechInfo->theRecognizer, kSRForegroundOnly, &forground, sizeof (forground));
  114.     }
  115.  
  116.     return theErr;
  117. }
  118.